var home = require("../../utils/util.js");
var url = home.home;
var url2 = home.home2;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: "",
    color: "color",
    type: 1,
    listData: [], //列表数据
    userId: home.getKey('userId'), //会员id
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    state: wx.getStorageSync('state'),
    states: false,
    btnState: true,
    goodsData:[],//充值订单数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      states:false 
    });
  },

  /* 直接到支付 *///仿原下单、新建充值订单表生成、小程序支付所需params
  Upgo(e){
    //console.log(e.currentTarget.dataset.inventory)
    wx.showLoading({
      title: '加载中...',
    });
    this.setData({
      btnState: false
    });
    let that = this;
    let goodsData = this.data.goodsData;
    wx.request({
      url: url + 'app/saveStockPayOrder',//新加小程序升级股东接口//参考user/order/info.js创建订单记录并生成pay_params_str（小程序支付所需params）
      data:{
        stockId:home.getKey('stockId'),
        storeId:home.getKey('storeId'),//店铺id
        memberId:home.getKey('userId'),//会员id
        payName:'升级股东充值',
        saobeiType:8,//7充值，8升级股东
        //recharge_type:"小程序支付",//用后面complate里的“微信支付”，区分后台充值订单：toback(1、充值 2、升级股东)
        id:e.currentTarget.dataset.id,//选择升级的股东级别id
        realAmount:e.currentTarget.dataset.inventory,//充值金额
        //payType:'微信支付',
        //staffId:'',
        //outTradeNo:''
      },
      method: 'POST',
      header: {
        "content-type": "application/json", // 默认
      },
      success:function (res) {
        if (res.data.code == 200) {
          wx.hideLoading({
            success: (res) => {
              that.setData({
                btnState: true,
              })
            },
          })
          goodsData = JSON.stringify(res.data.result);//返回res数据里区别出来是充值还是升级股东：toback(1、充值 2、升级股东)
          wx.redirectTo({
            //跳转到新增加的本目录下的确认支付页
            url: './complatey?goodsData='+encodeURIComponent(goodsData),
          });
        } else {
          wx.showToast({
            title: '发生错误',
            icon: 'none'
          })
        }
      }
    })
  },

  /**
   * 跳转订单确认页（不用）
   */
  toGoods(e) {
    let goodsId = e.currentTarget.dataset.id;
    let info = {};
    for (let i = 0; i < this.data.listData.length; i++) {
      if (this.data.listData[i].id == goodsId) {
        info = this.data.listData[i];
      }        
    }
    //转数据格式
    let goods = {};
    goods.goodsId = info.id;
    goods.commodityPic = info.productPic;
    goods.price = info.productPrice;
    goods.qty = 1;
    goods.commoditySpec = info.specification;//商品规格：1个
    goods.commodityName = info.tradeName;//商品名
    goods.discountAmount = 0;//折扣
    goods.type = 1;
    goods.price = info.productPrice;
    goods.totalPrice = info.productPrice;
    let data = []; 
    data.push(goods);
    //换到订单确认支付页
    wx.navigateTo({
      url: '../../user/order/info?data=' + JSON.stringify(data),
    });
    //wx.navigateTo({
    //  url: '../../shop/yuyue/index?info='+JSON.stringify(info),
    //})
  },


  //获取列表
  getShopList() {
    let requestUrl = '';
    let groupId = '';
    requestUrl = url + 'app/listStockGroup'//新加读取股东级别接口
    groupId = '1556545851230949377';
    let that = this;
    wx.request({
      url: requestUrl,
      data: {
        storeId: home.getKey('storeId'),
        groupId: groupId,
        memberId: home.getKey('userId'),
        keyword: that.data.value
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success: function (res) {
        if (res.data.code == 200) {
          let listData = that.data.listData;
          if (that.data.page == 1) {
            for (let i = 0; i < res.data.result.length; i++) {
              let pics = '';
              if (that.data.type == 1) {
                if (res.data.result[i].productPic) {
                  pics = res.data.result[i].productPic.split(',');
                  if (pics != '') {
                    res.data.result[i].pic = 'https://kyz.fbhkj.com//' + pics[0];
                  }
                }
              } else {
                if (res.data.result[i].projectPic) {
                  pics = res.data.result[i].projectPic.split(',');
                  if (pics != '') {
                    res.data.result[i].pic = 'https://kyz.fbhkj.com//' + pics[0];
                  }
                }
              }
            }
            listData = res.data.result;
          } else {
            for (let i = 0; i < res.data.result.length; i++) {
              let pics = '';
              if (that.data.type == 1) {
                if (res.data.result[i].productPic) {
                  pics = res.data.result[i].productPic.split(',');
                  if (pics != '') {
                    res.data.result[i].pic = 'https://kyz.fbhkj.com//' + pics[0];
                  }
                }
              } else {
                if (res.data.result[i].projectPic) {
                  pics = res.data.result[i].projectPic.split(',');
                  if (pics != '') {
                    res.data.result[i].pic = 'https://kyz.fbhkj.com//' + pics[0];
                  }
                }
              }
              listData.push(res.data.result[i]);
            }
          }
          that.setData({
            listData: listData
          });
        } else {
          home.wxMessage(res.data.message, 'none');
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      value:"",
      userId:home.getKey('userId'),
      page:1,
    })
    if (home.getKey('state') == 1) {
      this.setData({
        state: 1
      });
    } else if (home.getKey('state') == 2) {
      this.setData({
        state: 2
      });
    }
    this.getShopList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // let page = this.data.page;
    // this.setData({
    //   page: ++page
    // });
    // this.getShopList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})